from typing import Mapping
import requests
from urllib.parse import quote,unquote
import time
import datetime
from jsonpath import jsonpath
import json
from pymongo import MongoClient
import argparse
import pymongo
TmpResultData = {}
ResultData = []

def main():
    parser = argparse.ArgumentParser(description="RUN WEIBOAPI")
    # parser.add_argument('-c','--cookie')
    # parser.add_argument('-k','--keyword')
    # parser.add_argument('-n','--collection_name')
    parser.add_argument('-i','--id')
    args =  parser.parse_args()
    id = args.id
    mongourl = "mongodb://yuqing:Bigdata#2021@175.27.160.162:27017"
    client = MongoClient(mongourl)
    db = client["Task"]
    collection_task = db['task']
    condition = {'_id':id}
    task = collection_task.find_one(condition)
    info = task['canshu']
    info_json = json.loads(info)
    collection_name = info_json['dbname']
    keyword = info_json['kwd']
    start_time = info_json['fromdate']
    end_time = info_json['todate']
    weiboapi(start_time,end_time,keyword,collection_name,id)
def weiboapi(start_time,end_time,keyword,collection_name,id):
    mongourl = "mongodb://yuqing:Bigdata#2021@175.27.160.162:27017"
    # mongourl = "mongodb://yuqing:Bigdata#2021@118.195.192.186:27017"
    # mongourl = "mongodb://localhost:27017"
    client = MongoClient(mongourl)
    db = client["Task"]
    collection = db[collection_name]
    collection_task = db['task']
    start_time_array = time.strptime(f'{start_time} 00:00:00','%Y-%m-%d %H:%M:%S')
    start_time = int(round(time.mktime(start_time_array)*1000))
    end_time_array = time.strptime(f'{end_time} 00:00:00','%Y-%m-%d %H:%M:%S')
    end_time = int(round(time.mktime(end_time_array)*1000))
    data = {
        'q' :quote(keyword),
        'starttime':start_time,
        'endtime':end_time,
        'access_token':'2.00bgD1iCWeF3iC9da440e24eYIQs4E',
        'page':1,
        'count':50,
        'hasori':1
    }

    response = requests.get('https://c.api.weibo.com/2/search/statuses/limited.json',data)
    a = ('https://c.api.weibo.com/2/search/statuses/limited.json',data)
    print(response.url)
    r_json= response.json()
    count=jsonpath(r_json,'$.total_number')
    # pubtime = jsonpath(r_json,'$..created_at')
    # print(pubtime)
    Mapping = {
        'total_number':'$.total_number',
        'statuses':'$.statuses'
    }

    for key in Mapping.keys():
        data = jsonpath(r_json,Mapping.get(key))
        if data:
            TmpResultData[key] = data
        else:
            TmpResultData[key] = None
    informations = TmpResultData['statuses'][0]
    if informations:
        condition = {'_id':id}
        result = collection_task.update_one(condition,{'$set':{'status':'run'}})
        print(result.matched_count,result.modified_count)
        for information in informations:
            item={}
            item['id'] = information['user']['id']
            item['author'] = information['user']['name']
            item['authorLink'] = information['user']['url']
            item['focusOtherNumber'] = information['user']['friends_count']
            item['fansNumber'] = information['user']['followers_count']
            item['channel'] = 'weibo'
            item['reviewNumber'] = information['comments_count']
            item['retweetNumber'] = information['reposts_count']    
            # item['pictureSource'] = information['original_pic']
            item['keyword'] = keyword
            item['mainText'] = unquote(information['text'])
            item['source'] = information['source']
            item['pubTime'] = information['created_at']
            item['getTime'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            collection.insert_one(item)
            print(item)
        condition = {'_id':id}
        result = collection_task.update_one(condition,{'$set':{'status':'stop'}})
        client.close()
if __name__ == "__main__":
    main()
